  <template>
      <div class="edit-popup">
          <popup
              ref="popupRef"
              :title="popupTitle"
              :async="true"
              width="70%"
              :clickModalClose="true"
              @confirm="handleSubmit"
              @close="handleClose"
          >
              <el-form
                  ref="formRef"
                  :model="formData"
                  label-width="200px"
                  :rules="formRules"
                  :inline="true"
                  class="followUpEditformRef"
              >
                  <el-form-item label="跟进记录编号" prop="id">
                      <el-input v-model="formData.id" placeholder="请输入跟进记录编号" />
                  </el-form-item>
                  <el-form-item label="跟进记录名称" prop="name">
                      <el-input v-model="formData.name" placeholder="请输入跟进记录名称" />
                  </el-form-item>
                  <el-form-item label="跟进时间" prop="followUpTime">
                      <el-date-picker
                          class="flex-1 !flex"
                          v-model="formData.followUpTime"
                          type="datetime"
                          clearable
                          value-format="YYYY-MM-DD hh:mm:ss"
                          placeholder="请选择跟进时间"
                      />
                  </el-form-item>
                  <el-form-item label="所属模块" prop="module">
                      <el-select
                          class="flex-1"
                          v-model="formData.module"
                          placeholder="请选择所属模块"
                      >
                          <el-option label="全部" value="" />
                          <el-option label="线索" value="0" />
                          <el-option label="客户" value="1" />
                          <el-option label="商机" value="2" />
                          <el-option label="合同" value="3" />
                      </el-select>
                  </el-form-item>
                  <el-form-item label="跟进方式" prop="type">
                      <el-select class="flex-1" v-model="formData.type" placeholder="请选择跟进方式">
                          <el-option label="全部" value="" />
                          <el-option label="电话" value="0" />
                          <el-option label="QQ" value="1" />
                          <el-option label="拜访" value="2" />
                          <el-option label="微信" value="3" />
                          <el-option label="邮件" value="4" />
                          <el-option label="短信" value="5" />
                          <el-option label="其他" value="6" />
                      </el-select>
                  </el-form-item>
                  <el-form-item label="跟进状态" prop="status">
                      <el-select
                          class="flex-1"
                          v-model="formData.status"
                          placeholder="请选择跟进状态"
                      >
                          <el-option label="联系方式有效" value="1" />
                          <el-option label="未处理" value="2" />
                          <el-option label="联系方式无效" value="3" />
                      </el-select>
                  </el-form-item>
                  <el-form-item label="跟进人员" prop="followUpId">
                      <el-select
                          v-model="formData.followUpId"
                          class="flex-1"
                          placeholder="请选择跟进人员"
                      >
                          <el-option label="张三" value="1" />
                          <el-option label="李四" value="2" />
                          <el-option label="王五" value="3" />
                      </el-select>
                  </el-form-item>
                  <el-form-item label="联系人" prop="linkmanId">
                      <el-select
                          v-model="formData.linkmanId"
                          class="flex-1"
                          placeholder="请选择联系人"
                      >
                          <el-option label="张三" value="1" />
                          <el-option label="李四" value="2" />
                          <el-option label="王五" value="3" />
                      </el-select>
                  </el-form-item>
                  <el-form-item label="联系方式" prop="phone">
                      <el-input v-model="formData.phone" placeholder="请输入联系方式" />
                  </el-form-item>
                  <el-form-item label="上传附件" prop="fileId">
                      <el-input v-model="formData.fileId" placeholder="请输入上传附件" />
                  </el-form-item>

                  <el-form-item label="下次跟进时间" prop="nextFollowUpTime">
                      <daterange-picker
                          v-model:startTime="formData.nextFollowUpTime"
                          v-model:endTime="formData.nextFollowUpTime"
                      />
                  </el-form-item>
                  <el-form-item label="" prop=""> </el-form-item>
              </el-form>
              <el-form-item label="跟进内容" label-width="200px" prop="followUpContent">
                  <editor v-model="formData.followUpContent" :height="500" />
              </el-form-item>
          </popup>
      </div>
  </template>
  <script lang="ts" setup>
  import type { FormInstance } from 'element-plus'
  import { followupEdit, followupAdd, followupDetail } from '@/api/followup'
  import Popup from '@/components/popup/index.vue'
  import feedback from '@/utils/feedback'
  import type { PropType } from 'vue'
  defineProps({
      dictData: {
          type: Object as PropType<Record<string, any[]>>,
          default: () => ({})
      }
  })
  const emit = defineEmits(['success', 'close'])
  const formRef = shallowRef<FormInstance>()
  const popupRef = shallowRef<InstanceType<typeof Popup>>()
  const mode = ref('add')
  const popupTitle = computed(() => {
      return mode.value == 'edit' ? '编辑跟进记录' : '新增跟进记录'
  })

  const formData = reactive({
      id: '',
      name: '',
      followUpTime: '',
      module: '',
      type: '',
      status: '',
      followUpId: '',
      linkmanId: '',
      phone: '',
      fileId: '',
      followUpContent: '',
      nextFollowUpTime: ''
  })

  const formRules = {
      followUpTime: [
          {
              required: true,
              message: '请选择跟进时间',
              trigger: ['blur']
          }
      ],
      followUpContent: [
          {
              required: true,
              message: '请输入跟进内容',
              trigger: ['blur']
          }
      ]
  }

  const handleSubmit = async () => {
      await formRef.value?.validate()
      const data: any = { ...formData }
      mode.value == 'edit' ? await followupEdit(data) : await followupAdd(data)
      popupRef.value?.close()
      feedback.msgSuccess('操作成功')
      emit('success')
  }

  const open = (type = 'add') => {
      mode.value = type
      popupRef.value?.open()
  }

  const setFormData = async (data: Record<string, any>) => {
      for (const key in formData) {
          if (data[key] != null && data[key] != undefined) {
              //@ts-ignore
              formData[key] = data[key]
          }
      }
  }

  const getDetail = async (row: Record<string, any>) => {
      const data = await followupDetail({
          id: row.id
      })
      setFormData(data)
  }

  const handleClose = () => {
      emit('close')
  }

  defineExpose({
      open,
      setFormData,
      getDetail
  })
  </script>

  <style scoped>
  .followUpEditformRef .el-form-item {
      width: 400px;
  }

  ::v-deep .el-collapse-item__header {
    padding-left: 54px;
    background-color: var(--el-bg-color-collapse);
    font-size: 16px;
    font-weight: 600;
    color: var(--el-text-color-collapse);
  }

  ::v-deep .el-collapse-item__wrap {
      padding-top: 40px;
  }
  </style>
